
Partial Class ServerL
    Inherits System.Web.UI.Page

    Private moTreeView As New jTreeView.Tree

    Protected Sub RenderTree()
        HttpContext.Current.Response.Write(moTreeView.Render)
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then
            T.Value = Now

            If IsNumeric(DID.Value) Then
                Dim oDB As New SQLAutoDocLib.Database(DID.Value)
                oDB.Load()
                SID.Value = oDB.ServerID
            End If

            moTreeView.OpenedImageURL = "images/folderopen.gif"
            moTreeView.ClosedImageURL = "images/folderclose.gif"
            moTreeView.LeafImageURL = "images/nodesel.gif"
            moTreeView.LinkTarget = "Body"

            Dim oServerNode As New jTreeView.Node
            oServerNode.Name = "Servers"
            oServerNode.ID = "S"
            oServerNode.LinkURL = "SQAbout.aspx"
            oServerNode.LinkToolTip = "Add Server"
            oServerNode.IsExpanded = True

            Dim oFactory As New SQLAutoDocLib.Server_Factory

            For Each oServer As SQLAutoDocLib.Server In oFactory.AllServers
                Dim oNode As New jTreeView.Node
                oNode.Name = oServer.Name
                oNode.ID = oServer.ID
                oNode.LinkURL = "SQServerE.aspx?S=" & oServer.ID
                oNode.LinkToolTip = "Edit Server Info"

                If IsNumeric(SID.Value) AndAlso oServer.ID = SID.Value Then
                    oNode.IsExpanded = True
                End If

                oNode.ChildNodes = GetDatabasesForServer(oServer)

                oServerNode.ChildNodes.Add(oNode)
            Next

            moTreeView.RootNodes.Add(oServerNode)
        End If
    End Sub

    Private Function GetDatabasesForServer(ByVal oServer As SQLAutoDocLib.Server) As ArrayList
        Dim oList As New ArrayList

        Dim oFactory As New SQLAutoDocLib.Database_Factory
        For Each oDatabase As SQLAutoDocLib.Database In oFactory.GetAllDatabasesForServer(oServer.ID)
            Dim oNode As New jTreeView.Node

            oNode.Name = oDatabase.Name
            oNode.LinkToolTip = "Edit Database Info"
            oNode.LinkURL = "SQDatabaseE.aspx?S=" & oServer.ID & "&D=" & oDatabase.ID & "&DV=" & oDatabase.RevID

            If IsNumeric(DID.Value) AndAlso oDatabase.ID = DID.Value Then
                oNode.IsExpanded = True
            End If

            oNode.ChildNodes = GetDatabaseChildren(oServer, oDatabase)

            oList.Add(oNode)
        Next
        Return oList
    End Function

    Private Function GetDatabaseChildren(ByVal oServer As SQLAutoDocLib.Server, ByVal oDatabase As SQLAutoDocLib.Database) As ArrayList
        Dim oList As New ArrayList

        Dim oNode As jTreeView.Node

        oNode = New jTreeView.Node
        oNode.Name = "Size"
        oNode.LinkURL = "SQSizeE.aspx?S=" & oServer.ID & "&D=" & oDatabase.ID
        oNode.LinkToolTip = "View Size History"
        oList.Add(oNode)

        oNode = New jTreeView.Node
        oNode.Name = "Blocking"
        oNode.LinkURL = "SQBlockingE.aspx?S=" & oServer.ID & "&D=" & oDatabase.ID
        oNode.LinkToolTip = "View Blocking History"
        oList.Add(oNode)

        oNode = New jTreeView.Node
        oNode.Name = "Connections"
        oNode.LinkURL = "SQConnectionsE.aspx?D=" & oDatabase.ID & "&DT=" & HttpUtility.UrlEncode(Now.ToShortDateString)
        oNode.LinkToolTip = "View Connections History"
        oList.Add(oNode)

        oNode = New jTreeView.Node
        oNode.Name = "WaitStates"
        oNode.LinkURL = "SQWaitStatesE.aspx?S=" & oServer.ID & "&D=" & oDatabase.ID
        oNode.LinkToolTip = "View WaitStates History"
        oList.Add(oNode)

        oNode = New jTreeView.Node
        oNode.Name = "Schema"
        oNode.LinkURL = "SQSchemaE.aspx?S=" & oServer.ID & "&D=" & oDatabase.ID
        oNode.LinkToolTip = "View Schema Info"
        oNode.ChildNodes = GetSchemaChildren(oServer, oDatabase)
        oList.Add(oNode)

        Return oList
    End Function

    Private Function GetSchemaChildren(ByVal oServer As SQLAutoDocLib.Server, ByVal oDatabase As SQLAutoDocLib.Database) As ArrayList
        Dim oList As New ArrayList

        Dim oNode As jTreeView.Node
        oNode = New jTreeView.Node
        oNode.Name = "Logins"
        oNode.LinkURL = "SQLoginE.aspx?S=" & oServer.ID & "&D=" & oDatabase.ID
        oNode.LinkToolTip = "Edit Login Info"
        oList.Add(oNode)

        oNode = New jTreeView.Node
        oNode.Name = "Tables"
        oNode.LinkURL = "SQTableE.aspx?S=" & oServer.ID & "&D=" & oDatabase.ID
        oNode.LinkToolTip = "Edit Table Info"
        oList.Add(oNode)

        oNode = New jTreeView.Node
        oNode.Name = "Views"
        oNode.LinkURL = "SQViewE.aspx?S=" & oServer.ID & "&D=" & oDatabase.ID
        oNode.LinkToolTip = "Edit View Info"
        oList.Add(oNode)

        oNode = New jTreeView.Node
        oNode.Name = "Procedures"
        oNode.LinkURL = "SQSProcE.aspx?S=" & oServer.ID & "&D=" & oDatabase.ID
        oNode.LinkToolTip = "Edit Stored Procedure Info"
        oList.Add(oNode)

        oNode = New jTreeView.Node
        oNode.Name = "Functions"
        oNode.LinkURL = "SQFunctionE.aspx?S=" & oServer.ID & "&D=" & oDatabase.ID
        oNode.LinkToolTip = "Edit Function Info"
        oList.Add(oNode)

        Return oList
    End Function
End Class
